package orchestration.android.execution.tasks;

import java.util.ArrayList;
import java.util.List;

import orchestration.android.RunExecutionActivity;
import orchestration.android.execution.commands.CommandFactory;
import orchestration.android.execution.exceptions.TaskExecutionException;
import orchestration.android.execution.exceptions.UnknownOperationException;
import orchestration.persistence.exceptions.UnsupportedTypeException;
import orchestration.utils.PropertyUtils;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

/**
 * @author  <a href="mailto:car.ferrarini@gmail.com">Carlo Ferrarini</a>
 */
public class ContentProviderQueryTask extends AndroidTask {

	private static final String TAG="ContentProviderQueryTask";
	
	/**
	 * @uml.property  name="uri"
	 */
	private Uri uri;
	/**
	 * @uml.property  name="projection"
	 */
	private String[] projection;
	/**
	 * @uml.property  name="whereClause"
	 */
	private String whereClause;
	/**
	 * @uml.property  name="whereArgs"
	 */
	private String[] whereArgs;
	/**
	 * @uml.property  name="order"
	 */
	private String order;
	private List<SaveQueryResultOperation> operations;
	

	public ContentProviderQueryTask(String id) {
		super(id);
		this.operations=new ArrayList<SaveQueryResultOperation>();
	}
	
	/**
	 * @return
	 * @uml.property  name="uri"
	 */
	public Uri getUri() {
		return uri;
	}

	/**
	 * @param uri
	 * @uml.property  name="uri"
	 */
	public void setUri(Uri uri) {
		this.uri = uri;
	}

	/**
	 * @return
	 * @uml.property  name="projection"
	 */
	public String[] getProjection() {
		return projection;
	}

	/**
	 * @param projection
	 * @uml.property  name="projection"
	 */
	public void setProjection(String[] projection) {
		this.projection = projection;
	}

	/**
	 * @return
	 * @uml.property  name="whereClause"
	 */
	public String getWhereClause() {
		return whereClause;
	}

	/**
	 * @param whereClause
	 * @uml.property  name="whereClause"
	 */
	public void setWhereClause(String whereClause) {
		this.whereClause = whereClause;
	}

	/**
	 * @return
	 * @uml.property  name="whereArgs"
	 */
	public String[] getWhereArgs() {
		return whereArgs;
	}

	/**
	 * @param whereArgs
	 * @uml.property  name="whereArgs"
	 */
	public void setWhereArgs(String[] whereArgs) {
		this.whereArgs = whereArgs;
	}

	/**
	 * @return
	 * @uml.property  name="order"
	 */
	public String getOrder() {
		return order;
	}

	/**
	 * @param order
	 * @uml.property  name="order"
	 */
	public void setOrder(String order) {
		this.order = order;
	}
	public void addOperation(SaveQueryResultOperation operation){
		this.operations.add(operation);
	}
	
	@Override
	public void onExecute(RunExecutionActivity rea) throws TaskExecutionException {
		for(int i=0;i<whereArgs.length;i++){
			String arg=whereArgs[i];
			if(arg.charAt(0)=='$'){
				whereArgs[i]=PropertyUtils.getStringValue(rea.getProperty(arg.substring(1)));
			}
		}
		Log.i(TAG,"uri:"+ uri+ " where " +whereClause +" order" + order);
		Cursor c = rea.managedQuery(uri, projection, whereClause, whereArgs, order);
		Log.i(TAG,"retrieved a cursor with " + c.getCount() + " rows");
		rea.startManagingCursor(c);
		for(SaveQueryResultOperation sqro : operations)
			try {
				sqro.exec(c, rea);
			} catch (UnknownOperationException e) {
				throw new TaskExecutionException(e);
			} catch (UnsupportedTypeException e) {
				throw new TaskExecutionException(e);
			}finally{
				rea.stopManagingCursor(c);
				c.close();
			}
		rea.setCurrentCommand(CommandFactory.createCommand(CommandFactory.RESUME_EXECUTION));
		rea.executeNextCommand();
	}
	

}